package com.ruoyi.mobile.service.impl;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.mobile.domain.UserInfo;
import com.ruoyi.mobile.mapper.UserInfoMapper;
import com.ruoyi.mobile.model.vo.HuaweiHealthWeekStatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mobile.mapper.HuaweiHealthWeekStatMapper;
import com.ruoyi.mobile.domain.HuaweiHealthWeekStat;
import com.ruoyi.mobile.service.IHuaweiHealthWeekStatService;

/**
 * 华为健康每周统计数据Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-10-18
 */
@Service
public class HuaweiHealthWeekStatServiceImpl implements IHuaweiHealthWeekStatService 
{
    @Autowired
    private HuaweiHealthWeekStatMapper huaweiHealthWeekStatMapper;
    @Autowired
    public UserInfoMapper userInfoMapper;
    /**
     * 查询华为健康每周统计数据列表
     * 
     * @param huaweiHealthWeekStat 华为健康每周统计数据
     * @return 华为健康每周统计数据
     */
    @Override
    public List<HuaweiHealthWeekStat> selectHuaweiHealthWeekStatList(HuaweiHealthWeekStat huaweiHealthWeekStat)
    {
        return huaweiHealthWeekStatMapper.selectHuaweiHealthWeekStatList(huaweiHealthWeekStat);
    }

    @Override
    public List<HuaweiHealthWeekStatVo> selectHuaweiHealthWeekStatVoList(HuaweiHealthWeekStat huaweiHealthWeekStat)
    {
        List<HuaweiHealthWeekStatVo> list = huaweiHealthWeekStatMapper.selectHuaweiHealthWeekStatVoList(huaweiHealthWeekStat);
        for (HuaweiHealthWeekStatVo huaweiHealthWeekStatVo : list) {
            //获取出生日期
            LocalDate birthday = huaweiHealthWeekStatVo.getBirthday();
            //年龄
            Integer age = null;
            if (birthday == null) {
                age = null;
            } else {
                age = Period.between(birthday, LocalDate.now()).getYears();
            }
            //身高
            String height = "未填写";
            //体重
            String weight = "未填写";
            //获取用户基本信息
            UserInfo userInfo = userInfoMapper.selectByUserIdAndSubType(huaweiHealthWeekStatVo.getUserId(), 4, 4);
            if (userInfo != null) {
                try {
                    ObjectMapper objectMapper = new ObjectMapper();
                    Map<String, Object> map = objectMapper.readValue(userInfo.getContent(), Map.class);
                    height = (String) map.get("quest4_1");
                    weight = (String) map.get("quest4_2");
                } catch (Exception e) {

                }
            }
            huaweiHealthWeekStatVo.setHeight(height);
            huaweiHealthWeekStatVo.setWeight(weight);
            huaweiHealthWeekStatVo.setAge(age);
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.MM.dd");
            String formattedStatStartDate = huaweiHealthWeekStatVo.getStatStartDate().format(formatter);
            String formattedStatEndDate = huaweiHealthWeekStatVo.getStatEndDate().format(formatter);
            huaweiHealthWeekStatVo.setStatDate( formattedStatStartDate + "-" + formattedStatEndDate );
            String birthdayStr = "未填写";
            String ageStr = "未填写";
            if (birthday != null) {
                birthdayStr = huaweiHealthWeekStatVo.getBirthday().format(formatter);
                ageStr = String.valueOf(age);
            }
            huaweiHealthWeekStatVo.setBirthdayStr(birthdayStr);
            huaweiHealthWeekStatVo.setAgeStr(ageStr);
        }
        return list;
    }

    @Override
    public List<HuaweiHealthWeekStatVo> selectHuaweiHealthLastWeekStatVoList() {
        LocalDate endDate = LocalDateTimeUtil.offset(LocalDateTimeUtil.now().toLocalDate().atStartOfDay(),-1, ChronoUnit.DAYS).toLocalDate();
        LocalDate startDate = LocalDateTimeUtil.offset(endDate.atStartOfDay(),-6, ChronoUnit.DAYS).toLocalDate();
        List<HuaweiHealthWeekStatVo> list = huaweiHealthWeekStatMapper.selectHuaweiHealthLastWeekStatVoList(startDate, endDate);
        for (HuaweiHealthWeekStatVo huaweiHealthWeekStatVo : list) {
            //获取出生日期
            LocalDate birthday = huaweiHealthWeekStatVo.getBirthday();
            //年龄
            Integer age = null;
            if (birthday == null) {
                age = null;
            } else {
                age = Period.between(birthday, LocalDate.now()).getYears();
            }
            //身高
            String height = "未填写";
            //体重
            String weight = "未填写";
            //获取用户基本信息
            UserInfo userInfo = userInfoMapper.selectByUserIdAndSubType(huaweiHealthWeekStatVo.getUserId(), 4, 4);
            if (userInfo != null) {
                try {
                    ObjectMapper objectMapper = new ObjectMapper();
                    Map<String, Object> map = objectMapper.readValue(userInfo.getContent(), Map.class);
                    height = (String) map.get("quest4_1");
                    weight = (String) map.get("quest4_2");
                } catch (Exception e) {

                }
            }
            huaweiHealthWeekStatVo.setHeight(height);
            huaweiHealthWeekStatVo.setWeight(weight);
            huaweiHealthWeekStatVo.setAge(age);
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.MM.dd");
            String formattedStatStartDate = startDate.format(formatter);
            String formattedStatEndDate = endDate.format(formatter);
            huaweiHealthWeekStatVo.setStatDate( formattedStatStartDate + "-" + formattedStatEndDate );
            String birthdayStr = "未填写";
            String ageStr = "未填写";
            if (birthday != null) {
                birthdayStr = huaweiHealthWeekStatVo.getBirthday().format(formatter);
                ageStr = String.valueOf(age);
            }
            huaweiHealthWeekStatVo.setBirthdayStr(birthdayStr);
            huaweiHealthWeekStatVo.setAgeStr(ageStr);
            if (huaweiHealthWeekStatVo.getMinHeartrate() == null) {
                huaweiHealthWeekStatVo.setMinHeartrate(new BigDecimal(0));
            }
        }
        return list;
    }

}
